Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Truncated binary encoding</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Truncated_binary_encoding"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.math.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Truncated_binary_encoding rootpage-Truncated_binary_encoding skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Truncated binary encoding</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><style data-mw-deduplicate="TemplateStyles:r1305433154">
/* start https://en.wikipedia.org/ */


.mw-parser-output .ambox{border:1px solid #a2a9b1;border-left:10px solid #36c;background-color:#fbfbfb;box-sizing:border-box}.mw-parser-output .ambox+link+.ambox,.mw-parser-output .ambox+link+style+.ambox,.mw-parser-output .ambox+link+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+style+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+link+.ambox{margin-top:-1px}html body.mediawiki .mw-parser-output .ambox.mbox-small-left{margin:4px 1em 4px 0;overflow:hidden;width:238px;border-collapse:collapse;font-size:88%;line-height:1.25em}.mw-parser-output .ambox-speedy{border-left:10px solid #b32424;background-color:#fee7e6}.mw-parser-output .ambox-delete{border-left:10px solid #b32424}.mw-parser-output .ambox-content{border-left:10px solid #f28500}.mw-parser-output .ambox-style{border-left:10px solid #fc3}.mw-parser-output .ambox-move{border-left:10px solid #9932cc}.mw-parser-output .ambox-protection{border-left:10px solid #a2a9b1}.mw-parser-output .ambox .mbox-text{border:none;padding:0.25em 0.5em;width:100%}.mw-parser-output .ambox .mbox-image{border:none;padding:2px 0 2px 0.5em;text-align:center}.mw-parser-output .ambox .mbox-imageright{border:none;padding:2px 0.5em 2px 0;text-align:center}.mw-parser-output .ambox .mbox-empty-cell{border:none;padding:0;width:1px}.mw-parser-output .ambox .mbox-image-div{width:52px}@media(min-width:720px){.mw-parser-output .ambox{margin:0 10%}}@media print{body.ns-0 .mw-parser-output .ambox{display:none!important}}


/* end https://en.wikipedia.org/ */
</style>
<p><b>Truncated binary encoding</b> is an <a href="Entropy_encoding" class="mw-redirect" title="Entropy encoding">entropy encoding</a> typically used for uniform <a href="Probability_distribution" title="Probability distribution">probability distributions</a> with a finite alphabet. It is parameterized by an alphabet with total size of number <i>n</i>. It is a slightly more general form of <a href="Binary_numeral_system" class="mw-redirect" title="Binary numeral system">binary encoding</a> when <i>n</i> is not a <a href="Power_of_two" title="Power of two">power of two</a>.
</p><p>If <i>n</i> is a power of two, then the coded value for 0 ≤ <i>x</i> &lt; <i>n</i> is the simple binary code for <i>x</i> of length log<sub>2</sub>(<i>n</i>). Otherwise let <i>k</i> = floor(log<sub>2</sub>(<i>n</i>)), such that 2<sup><i>k</i></sup> &lt; <i>n</i> &lt; 2<sup><i>k</i>+1</sup>and let <i>u</i> = 2<sup><i>k</i>+1</sup> − <i>n</i>.
</p><p>Truncated binary encoding assigns the first <i>u</i> symbols codewords of length <i>k</i> and then assigns the remaining <i>n</i> − <i>u</i> symbols the last <i>n</i> − <i>u</i> codewords of length <i>k</i> + 1. Because all the codewords of length <i>k</i> + 1 consist of an unassigned codeword of length <i>k</i> with a "0" or "1" appended, the resulting code is a <a href="Prefix_code" title="Prefix code">prefix code</a>.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="History">History</h2></div>
<p>Used since at least 1984, <b>phase-in codes</b>, also known as <b>economy codes</b>,<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> are also known as truncated binary encoding.
</p>
<div class="mw-heading mw-heading2"><h2 id="Example_with_n_=_5">Example with <i>n</i> = 5</h2></div>
<p>For example, for the alphabet {0, 1, 2, 3, 4}, <i>n</i> = 5 and 2<sup>2</sup> ≤ <i>n</i> &lt; 2<sup>3</sup>, hence <i>k</i> = 2 and <i>u</i> = 2<sup>3</sup> − 5 = 3. Truncated binary encoding assigns the first <i>u</i> symbols the codewords 00, 01, and 10, all of length 2, then assigns the last <i>n</i> − <i>u</i> symbols the codewords 110 and 111, the last two codewords of length 3.
</p><p>For example, if <i>n</i> is 5, plain binary encoding and truncated binary encoding allocates the following <a href="Code_word_(communication)" title="Code word (communication)">codewords</a>. Digits shown <del>struck</del> are not transmitted in truncated binary.
</p>
<table class="wikitable" style="text-align:center">

<tbody><tr>
<th>Truncated<br>binary</th>
<th colspan="3">Encoding</th>
<th>Standard<br>binary
</th></tr>
<tr>
<td align="right">0</td>
<td bgcolor="silver"><del>0</del></td>
<td>0</td>
<td>0</td>
<td align="left">0
</td></tr>
<tr>
<td align="right">1</td>
<td bgcolor="silver"><del>0</del></td>
<td>0</td>
<td>1</td>
<td align="left">1
</td></tr>
<tr>
<td align="right">2</td>
<td bgcolor="silver"><del>0</del></td>
<td>1</td>
<td>0</td>
<td align="left">2
</td></tr>
<tr>
<td align="right">UNUSED</td>
<td bgcolor="silver"><del>0</del></td>
<td bgcolor="silver"><del>1</del></td>
<td bgcolor="silver"><del>1</del></td>
<td align="left">3
</td></tr>
<tr>
<td align="right">UNUSED</td>
<td bgcolor="silver"><del>1</del></td>
<td bgcolor="silver"><del>0</del></td>
<td bgcolor="silver"><del>0</del></td>
<td align="left">4
</td></tr>
<tr>
<td align="right">UNUSED</td>
<td bgcolor="silver"><del>1</del></td>
<td bgcolor="silver"><del>0</del></td>
<td bgcolor="silver"><del>1</del></td>
<td align="left">5/UNUSED
</td></tr>
<tr>
<td align="right">3</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td align="left">6/UNUSED
</td></tr>
<tr>
<td align="right">4</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td align="left">7/UNUSED
</td></tr></tbody></table>
<p>It takes 3 bits to encode <i>n</i> using straightforward binary encoding, hence 2<sup>3</sup> − <i>n</i> = 8 − 5 = 3 are unused.
</p><p>In numerical terms, to send a value <i>x</i>, where 0 ≤ <i>x</i> &lt; <i>n</i>, and where there are 2<sup><i>k</i></sup> ≤ <i>n</i> &lt; 2<sup><i>k</i>+1</sup> symbols, there are <i>u</i> = 2<sup><i>k</i>+1</sup> − <i>n</i> unused entries when the alphabet size is rounded up to the nearest power of two. The process to encode the number <i>x</i> in truncated binary is: if <i>x</i> is less than <i>u</i>, encode it in <i>k</i> binary bits; if <i>x</i> is greater than or equal to <i>u</i>, encode the value <i>x</i> + <i>u</i> in <i>k</i> + 1 binary bits.
</p>
<div class="mw-heading mw-heading2"><h2 id="Example_with_n_=_10">Example with <i>n</i> = 10</h2></div>
<p>Another example, encoding an alphabet of size 10 (between 0 and 9) requires 4&nbsp;bits, but there are 2<sup>4</sup> − 10 = 6 unused codes, so input values less than 6 have the first bit discarded, while input values greater than or equal to 6 are offset by 6 to the end of the binary space. (Unused patterns are not shown in this table.)
</p>
<table class="wikitable" style="text-align:center">

<tbody><tr>
<th>Input<br>value</th>
<th>Offset</th>
<th>Offset<br>value</th>
<th>Standard<br>binary</th>
<th>Truncated<br>binary
</th></tr>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
<td><del>0</del>000</td>
<td>000
</td></tr>
<tr>
<td>1</td>
<td>0</td>
<td>1</td>
<td><del>0</del>001</td>
<td>001
</td></tr>
<tr>
<td>2</td>
<td>0</td>
<td>2</td>
<td><del>0</del>010</td>
<td>010
</td></tr>
<tr>
<td>3</td>
<td>0</td>
<td>3</td>
<td><del>0</del>011</td>
<td>011
</td></tr>
<tr>
<td>4</td>
<td>0</td>
<td>4</td>
<td><del>0</del>100</td>
<td>100
</td></tr>
<tr>
<td>5</td>
<td>0</td>
<td>5</td>
<td><del>0</del>101</td>
<td>101
</td></tr>
<tr>
<td colspan="5">
</td></tr>
<tr>
<td>6</td>
<td>6</td>
<td>12</td>
<td>0110</td>
<td>1100
</td></tr>
<tr>
<td>7</td>
<td>6</td>
<td>13</td>
<td>0111</td>
<td>1101
</td></tr>
<tr>
<td>8</td>
<td>6</td>
<td>14</td>
<td>1000</td>
<td>1110
</td></tr>
<tr>
<td>9</td>
<td>6</td>
<td>15</td>
<td>1001</td>
<td>1111
</td></tr></tbody></table>
<p>To decode, read the first <i>k</i> bits. If they encode a value less than <i>u</i>, decoding is complete. Otherwise, read an additional bit and subtract <i>u</i> from the result.
</p>
<div class="mw-heading mw-heading2"><h2 id="Example_with_n_=_7">Example with <i>n</i> = 7</h2></div>
<p>Here is a more extreme case: with <i>n</i> = 7 the next power of 2 is 8, so <i>k</i> = 2 and <i>u</i> = 2<sup>3</sup> − 7 = 1:
</p>
<table class="wikitable" style="text-align:center">

<tbody><tr>
<th>Input<br>value</th>
<th>Offset</th>
<th>Offset<br>value</th>
<th>Standard<br>binary</th>
<th>Truncated<br>binary
</th></tr>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
<td><del>0</del>00</td>
<td>00
</td></tr>
<tr>
<td colspan="5">
</td></tr>
<tr>
<td>1</td>
<td>1</td>
<td>2</td>
<td>001</td>
<td>010
</td></tr>
<tr>
<td>2</td>
<td>1</td>
<td>3</td>
<td>010</td>
<td>011
</td></tr>
<tr>
<td>3</td>
<td>1</td>
<td>4</td>
<td>011</td>
<td>100
</td></tr>
<tr>
<td>4</td>
<td>1</td>
<td>5</td>
<td>100</td>
<td>101
</td></tr>
<tr>
<td>5</td>
<td>1</td>
<td>6</td>
<td>101</td>
<td>110
</td></tr>
<tr>
<td>6</td>
<td>1</td>
<td>7</td>
<td>110</td>
<td>111
</td></tr></tbody></table>
<p>This last example demonstrates that a leading zero bit does not always indicate a short code; if <i>u</i> &lt; 2<sup><i>k</i></sup>, some long codes will begin with a zero bit.
</p>
<div class="mw-heading mw-heading2"><h2 id="Simple_algorithm">Simple algorithm</h2></div>
<p>Generate the truncated binary encoding for a value <i>x</i>, 0 ≤ <i>x</i> &lt; <i>n</i>, where <i>n</i> &gt; 0 is the size of the alphabet containing <i>x</i>. <i>n</i> need not be a power of two.
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="n">string</span><span class="w"> </span><span class="nf">TruncatedBinary</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="c1">// Set k = floor(log2(n)), i.e., k such that 2^k &lt;= n &lt; 2^(k+1).</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">t</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">k</span><span class="o">++</span><span class="p">;</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">&gt;&gt;=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>

<span class="w"> </span><span class="c1">// Set u to the number of unused codewords = 2^(k+1) - n.</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>

<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">u</span><span class="p">)</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">Binary</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">k</span><span class="p">);</span><span class="w"> </span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">Binary</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">u</span><span class="p">,</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">));</span>
<span class="p">}</span>
</pre></div>
<p>The routine <code>Binary</code> is expository; usually just the rightmost <code>len</code> bits of the variable <i>x</i> are desired.
Here we simply output the binary code for <i>x</i> using <code>len</code> bits, padding with high-order 0s if necessary.
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="n">string</span><span class="w"> </span><span class="nf">Binary</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">len</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">""</span><span class="p">;</span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">even</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="sc">'0'</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">s</span><span class="p">;</span>
<span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="sc">'1'</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">s</span><span class="p">;</span>
<span class="w"> </span>
<span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">&gt;&gt;=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">s</span><span class="p">.</span><span class="n">Length</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">len</span><span class="p">)</span>
<span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="sc">'0'</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">s</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">s</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="On_efficiency">On efficiency</h2></div>
<p>If <i>n</i> is not a power of two, and <i>k</i>-bit symbols are observed with probability <i>p</i>, then (<i>k</i> + 1)-bit symbols are observed with probability 1 − <i>p</i>. We can calculate the expected number of bits per symbol <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle b_{e}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msub>
<mi>b</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>e</mi>
</mrow>
</msub>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle b_{e}}</annotation>
</semantics>
</math></span><img src="./975a5d457db9b1ce726aba01dc15c502ca3b9d89.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.671ex; width:1.996ex; height:2.509ex;" alt="{\displaystyle b_{e}}" loading="lazy"></span> as
</p>
<dl><dd><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle b_{e}=pk+(1-p)(k+1).}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msub>
<mi>b</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>e</mi>
</mrow>
</msub>
<mo>=</mo>
<mi>p</mi>
<mi>k</mi>
<mo>+</mo>
<mo stretchy="false">(</mo>
<mn>1</mn>
<mo>−<!-- − --></mo>
<mi>p</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">(</mo>
<mi>k</mi>
<mo>+</mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
<mo>.</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle b_{e}=pk+(1-p)(k+1).}</annotation>
</semantics>
</math></span><img src="./59521d3383fe08c10db271e6e0258497539ffbff.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:24.967ex; height:2.843ex;" alt="{\displaystyle b_{e}=pk+(1-p)(k+1).}" loading="lazy"></span></dd></dl>
<p>Raw encoding of the symbol has <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle b_{u}=k+1}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msub>
<mi>b</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>u</mi>
</mrow>
</msub>
<mo>=</mo>
<mi>k</mi>
<mo>+</mo>
<mn>1</mn>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle b_{u}=k+1}</annotation>
</semantics>
</math></span><img src="./db6196fe3d98b9abc2a060f96e65aefd0ace67ea.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.671ex; width:10.483ex; height:2.509ex;" alt="{\displaystyle b_{u}=k+1}" loading="lazy"></span> bits. Then relative space saving <i>s</i> (see <a href="Data_compression_ratio" title="Data compression ratio">Data compression ratio</a>) of the encoding can be defined as
</p>
<dl><dd><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle s=1-{\frac {b_{e}}{b_{u}}}=1-{\frac {pk+(1-p)(k+1)}{k+1}}.}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>s</mi>
<mo>=</mo>
<mn>1</mn>
<mo>−<!-- − --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<msub>
<mi>b</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>e</mi>
</mrow>
</msub>
<msub>
<mi>b</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>u</mi>
</mrow>
</msub>
</mfrac>
</mrow>
<mo>=</mo>
<mn>1</mn>
<mo>−<!-- − --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mrow>
<mi>p</mi>
<mi>k</mi>
<mo>+</mo>
<mo stretchy="false">(</mo>
<mn>1</mn>
<mo>−<!-- − --></mo>
<mi>p</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">(</mo>
<mi>k</mi>
<mo>+</mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
</mrow>
<mrow>
<mi>k</mi>
<mo>+</mo>
<mn>1</mn>
</mrow>
</mfrac>
</mrow>
<mo>.</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle s=1-{\frac {b_{e}}{b_{u}}}=1-{\frac {pk+(1-p)(k+1)}{k+1}}.}</annotation>
</semantics>
</math></span><img src="./883202942a2e2020c55c917daf2657a9d43c9a1c.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -2.338ex; width:39.008ex; height:6.176ex;" alt="{\displaystyle s=1-{\frac {b_{e}}{b_{u}}}=1-{\frac {pk+(1-p)(k+1)}{k+1}}.}" loading="lazy"></span></dd></dl>
<p>When simplified, this expression leads to
</p>
<dl><dd><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle s={\frac {p}{k+1}}={\frac {p}{b_{u}}}.}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>s</mi>
<mo>=</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>p</mi>
<mrow>
<mi>k</mi>
<mo>+</mo>
<mn>1</mn>
</mrow>
</mfrac>
</mrow>
<mo>=</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>p</mi>
<msub>
<mi>b</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>u</mi>
</mrow>
</msub>
</mfrac>
</mrow>
<mo>.</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle s={\frac {p}{k+1}}={\frac {p}{b_{u}}}.}</annotation>
</semantics>
</math></span><img src="./8febb04c62b729febd1d6c930ff01f9bd7b91151.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -2.338ex; width:16.991ex; height:5.343ex;" alt="{\displaystyle s={\frac {p}{k+1}}={\frac {p}{b_{u}}}.}" loading="lazy"></span></dd></dl>
<p>This indicates that relative efficiency of truncated binary encoding increases as probability <i>p</i> of <i>k</i>-bit symbols increases, and the raw-encoding symbol bit-length <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle b_{u}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msub>
<mi>b</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>u</mi>
</mrow>
</msub>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle b_{u}}</annotation>
</semantics>
</math></span><img src="./eaa5c6e69f02d1037f23ecaeb9d05a9e18ef2f24.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.671ex; width:2.17ex; height:2.509ex;" alt="{\displaystyle b_{u}}" loading="lazy"></span> decreases.
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Benford's_law" title="Benford's law">Benford's law</a></li>
<li><a href="Golomb_coding" title="Golomb coding">Golomb coding</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */


.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}


/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text">Eastman, Willard L, <i>et al.</i> (Aug. 1984) <a rel="nofollow" class="external text" href="https://patentimages.storage.googleapis.com/86/7d/56/dd49314023387d/US4464650.pdf">Apparatus and Method for Compressing Data Signals and Restoring the Compressed Data Signals</a>, US Patent 4,464,650.</span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text">Acharya, Tinku et Já Já, Joseph F. (oct. 1996), <a rel="nofollow" class="external text" href="https://www.sciencedirect.com/science/article/abs/pii/0020025596000898">An on-line variable-length binary encoding of text</a>, Information Sciences, vol 94 no 1-4, p. 1-22.</span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text">Job van der Zwan. <a rel="nofollow" class="external text" href="https://observablehq.com/@jobleonard/phase-in-codes">"Phase-in Codes"</a>.</span>
</li>
</ol></div></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-03-24" href="https://en.wikipedia.org/wiki/?title=Truncated_binary_encoding&amp;oldid=1282068246">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>

</body></html>